(function(){
    "use strict";
    var acceptanceController=function ($scope,$http,$modal,acceptanceService,objectIdService){
        //项目ID号
        $scope.rootParasGet1 = function (){
            $scope.projectId = objectIdService('0');
            console.log($scope.projectId);
            $scope.find();
        };
        //$scope.projectId = "58db0ffdc128f729567773f8";

        $scope.find = function () {
            acceptanceService.find({projectId:$scope.projectId}).success(function (data) {
                if (data.result.length > 0 ){
                    $scope.list = data.result;
                } else {
                    $scope.list = [];
                }
            });
        };


        $scope.download = function (item) {
            var param = {
                id:item.id
            };
            if (item.status == false){
                alert("没有可以下载的文件！")
            }
            acceptanceService.download(param).success(function (data) {
               if (data.errorMsg != null || data.errosMsg != undefined){
                   alert(data.errorMsg);
               } else {
                   var filePath = data.filePath;
                   console.log(filePath);
                   //特殊字符+，&转义处理
                   filePath = filePath.replace(/\+/g, "%2B");
                   filePath = filePath.replace(/\&/g, "%26");
                   window.location.href = "/api/acceptance/download?filePath=" + filePath;
               }
            });
        };

        $scope.upload = function (item) {
            //弹出窗口
            var modalInstance = $modal.open({
                templateUrl: "upload.html",
                controller:uploadController,
                size:"lg",
                resolve: {
                    items: function () {
                        return {
                            item:item
                        };
                    }
                }
            });

            modalInstance.result.then(function (data) {
                $scope.find();
            });
        };
    };

    var uploadController=["$scope","$modal", "$modalInstance","items","AcceptanceService",
        function ($scope, $modal,$modalInstance,items,acceptanceService) {
            $scope.item = items.item;
            $scope.cancel = function () {
                $modalInstance.dismiss('cancel');
            };
            $scope.upload = function () {
                var fd = new FormData();
                var files = document.querySelector('input[type=file]').files;
                if(files.length == 0){
                    $scope.loading = false;
                    $.Huimodalalert("请选择要上传的文件",1000);
                    return;
                }
                angular.forEach(files,function (file) {
                    fd.append('file[]', file);
                });
                fd.append('id',$scope.item.id);
                acceptanceService.upload(fd).success(function (data) {
                    if(data.errorMsg != null){
                        alert(data.errorMsg);
                    } else {
                        alert("上传成功！");
                        //关闭
                        $modalInstance.close($scope.item);
                    }
                });
            };
        }];

    angular.module("cicd.acceptance",[])
        .controller("AcceptanceController",["$scope","$http","$modal","AcceptanceService","objectIdService",acceptanceController])

})();
